//===============================================================================
// Microsoft patterns & practices
// Web Service Software Factory
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Microsoft.Practices.RepositoryFactory.SchemaDiscovery.ObjectModel.Base;
using Microsoft.Practices.RepositoryFactory.SchemaDiscovery.Translators.Oracle.Data;
using Microsoft.Practices.RepositoryFactory.SchemaDiscovery.Translators.Oracle.Helpers;
using Microsoft.Practices.RepositoryFactory.SchemaDiscovery.ObjectModel.Oracle;
using System.Data.Common;

#endregion

namespace Microsoft.Practices.RepositoryFactory.SchemaDiscovery.Translators.Oracle
{
    internal class OracleTableTranslator
    {
        #region Public Implementation
        public OracleTable OracleTableCollectionToTable(DataRow oracleTable, DataTable oracleColumnsCollections, DataTable oracleForeignKeysCollection)
        {
            OracleTable table = new OracleTable();

            table.Name = oracleTable["table_name"].ToString();
            table.Schema = oracleTable["owner"].ToString();

            DataRow[] rows = oracleColumnsCollections.Select("", "Column_Name ASC");

            foreach(DataRow row in rows)
            {
                OracleColumn column = new OracleColumn();
                OracleDbDataType dbDataType = new OracleDbDataType();

                column.Name = row["Column_Name"].ToString();
                //column.IsNullable = OracleTranslatorHelper.IsNullable(row["nullable"]);
                column.IsPrimaryKey = (bool)row["IsPrimaryKey"];
                column.IsUnique = (bool)row["IsUniqueKey"];
                column.IsIdentity = (bool)row["IsIdentity"];
                column.IsReadOnly = (bool)row["IsReadOnly"];

                dbDataType.ProviderType = OracleDataTypeConverter.String2DatabaseType(row["Data_Type"].ToString());
                dbDataType.Type = OracleDataTypeConverter.DatabaseType2NetType(dbDataType.ProviderType);
                dbDataType.Precision = OracleTranslatorHelper.GetPrecision(row["data_Precision"]);
                dbDataType.Scale = OracleTranslatorHelper.GetScale(row["data_scale"]);
                dbDataType.Size = OracleTranslatorHelper.GetParameterSize(dbDataType.ProviderType, row);
                //dbDataType.Size = OracleTranslatorHelper.GetSize(row["Data_length"]);

                column.DbDataType = dbDataType;
                table.Columns.Add(column);
            }

            foreach(DataRow row in oracleForeignKeysCollection.Rows)
            {
                string columnName = row["Column_Name"].ToString();

                table.Columns.Find(
                    delegate(Column column)
                    { return (column.Name.Equals(columnName)); }).IsForeignKey = true;
            }

            return table;
        } 
        #endregion

        #region Private Implementation
        private bool NameMatch(Column column)
        {
            return column.Name.Equals("1");
        }

#endregion
    }
}